class Login {
    constructor() {
        this.username = document.querySelector('#recipient-username')
        this.password = document.querySelector('#recipient-password')
        this.btn = document.querySelector('.btn')
        this.box = document.querySelector('.box input')
        this.checkedBox()
        
      
        this.handleClick()
        this.initValue()
        this.ischecked = false

    }
    // 登录回显
    initValue(){
        if(localStorage.getItem('init')==undefined){
            this.username.value=''
            this.password.value=''
            this.box.checked=false
            

        }else{
           
            let obj1= JSON.parse( localStorage.getItem('init'))
         console.log(obj1.username)
         this.username.value=obj1.username
    
         this.password.value=obj1.password
        this.box.checked=obj1.checked 

        
           
        }
    }

    checkedBox(){
        this.box.onclick=()=>{
           
            if(this.username.value==''||this.password.value==''){
                alert('用户名或密码不能为空')
                return false
            }
            //    console.log(this.username.value)
            //  console.log(this.password.value)
            // console.log(this.box.checked)
            // 这里checked等于true
            var obj={
                username: this.username.value,
            
                password: this.password.value,
                checked:this.box.checked
             
            

            }
            console.log(obj)
    
           
            if(this.box.checked){
                // console.log(this.box.checked)
                localStorage.setItem('init',JSON.stringify(obj))
               
                
            }else{
                localStorage.removeItem('init')
               

            }
  
        }
    }
   
    handleClick() {
        this.btn.onclick = () => {

            var arr = JSON.parse(localStorage.getItem('info')) || []
            if (arr.length == 0) {
                alert('该账号还未注册，请到注册页面注册！')
                location.href = 'register.html'
                return
            } else {
                //  console.log(this.username.value)
                //  console.log(this.password.value)
                arr.forEach(item => {
                    if (item.username == this.username.value) {
                        if (item.password == this.password.value) {
                            this.ischecked = true
                        }
                    }

                })
                console.log(this.ischecked)
                if (this.ischecked) {
                   
                    alert('登陆成功，即将跳转到首页')
                    location.href = '../index.html'
                } else {
                    alert('用户名或者密码错误，请重新输入！')
                }
              
            }

        }
    }
}
new Login()